package com.wzn.service;

import javax.annotation.Resource;

import com.dangdang.ddframe.rdb.sharding.api.HintManager;
import com.google.common.collect.Lists;
import com.wzn.entity.Order;
import com.wzn.respository.OrderRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Transactional
public class OrderService {

    @Resource
    private OrderRepository orderRepository;

    @Transactional(readOnly = true)
    public void select() {
        List<Order> list = orderRepository.selectAll();
        for (Order order : list) {
            System.out.println(order);
        }
        System.out.println("总条数:"+list.size());
    }

    public void clear() {
        orderRepository.deleteAll();
    }

    public void fooService() {
        Order criteria = new Order();
        criteria.setUserId(10);
        criteria.setOrderId(1);
        criteria.setStatus("INSERT");
        orderRepository.insert(criteria);
        criteria.setUserId(11);
        criteria.setOrderId(1);
        criteria.setStatus("INSERT2");
        orderRepository.insert(criteria);
       // orderRepository.update(Lists.newArrayList(10, 11));
    }

    public void fooServiceWithFailure() {
        fooService();
        throw new IllegalArgumentException("failed");
    }

    @Transactional(readOnly = true)
    public void selectAllFromDS0(){
        Map<String,Integer> map = new HashMap<>(2);
        map.put("userId",10);
        map.put("orderId",1000);

        List<Order> list = orderRepository.selectAllFromDs0(map);
        for (Order order : list) {
            System.out.println("order:"+order);
        }
    }
}
